from flask import Blueprint, make_response, jsonify, request, g
from flask_restful import Resource


import math

from setting import  login_required_api
from apps.dbConnect.dbCurd import dbCurdD



project = Blueprint('basic', __name__)

class salmListView(Resource):
    @login_required_api
    def get(self):
        # 业务员类型 a.OperatorType = 9
        args = request.args
        search = args.get('search', '')
        page = int(args.get('page', 1))
        operatortype = args.get('operatortype', None)
        pmr = {}
        pageSize = 50
        pmr['pageSize'] = pageSize
        pmr['page'] = page
        parmStr = ''
        if search != '':
            pmr['name'] = "%" + search + "%"
            parmStr = " and (name like :name or sal_no like :name)"
        count_sql = """
select count(*) as TOTAL from salm where sal_no!='' {0}
            """.format(parmStr)
        count_d, err = dbCurdD.dbOneJson(count_sql, pmr)
        # print(count_d)
        # print(count_sql)
        if err != None:
            return make_response(jsonify({'msg': '统计查询失败:' + err}), 202)
        if count_d['total'] == 0:
            return make_response(jsonify({'msg': '没有查询到数据'}), 202)
        # 计算总分页
        totalPage = int(math.ceil(count_d['total'] / pageSize))
        if page > int(totalPage):
            return make_response(jsonify({'msg': '没有跟多数据了'}), 202)

        list_sql = """
SELECT  * 
FROM (select ROW_NUMBER() OVER(ORDER BY sal_no DESC) AS ROWNUMBER, sal_no AS VALUE,sal_no+';'+name AS LABEL,0 AS [SELECT] from salm where sal_no!='' {0}) TEMP_ROW
WHERE ROWNUMBER>((:page - 1) * :pageSize) and ROWNUMBER<=((:page) * :pageSize)
            """.format(parmStr)
        # print(list_sql)
        # print(pmr)
        list_d, err = dbCurdD.dbListJon(list_sql, pmr)
        if err != None:
            # print(err)
            return make_response(jsonify({'msg': '明细查询失败:' + err}), 202)
        # print(data)
        # dataList = [
        #     {"ti_no":"TI20230609001","cus_no":"a0001","cus_name":"广州天心软件科技有限公司","ti_dd":"2023-06-09","select":False},
        #     {"ti_no":"TI20230609002","cus_no":"a0002","cus_name":"深圳天心软件科技有限公司aaaadasdsadasdarewasfsfsdf","ti_dd":"2023-06-10","select":False}
        # ]
        previousPage = False if page <= 1 else True
        nextPage = False if page >= totalPage else True
        result = {}
        result['data'] = list_d
        # 分页大小
        result['pageSize'] = pageSize
        # 是否有上一页
        result['previousPage'] = previousPage
        # 是否有下一页
        result['nextPage'] = nextPage
        # 总分页
        result['totalPage'] = totalPage
        # 当前页
        result['page'] = page
        # 总记录
        result['total'] = count_d['total']
        msg = {'msg': '成功', "result": result}
        return make_response(jsonify(msg), 200)

class zcNoListView(Resource):
    @login_required_api
    def get(self):
        # 业务员类型 a.OperatorType = 9
        args = request.args
        search = args.get('search', '')
        page = int(args.get('page', 1))
        operatortype = args.get('operatortype', None)
        pmr = {}
        pageSize = 50
        pmr['pageSize'] = pageSize
        pmr['page'] = page
        parmStr = ''
        if search != '':
            pmr['name'] = "%" + search + "%"
            parmStr = " and (name like :name or zc_no like :name)"
        count_sql = """
select count(*) as TOTAL from zc_no where zc_no!='' {0}
            """.format(parmStr)
        count_d, err = dbCurdD.dbOneJson(count_sql, pmr)
        # print(count_d)
        # print(count_sql)
        if err != None:
            return make_response(jsonify({'msg': '统计查询失败:' + err}), 202)
        if count_d['total'] == 0:
            return make_response(jsonify({'msg': '没有查询到数据'}), 202)
        # 计算总分页
        totalPage = int(math.ceil(count_d['total'] / pageSize))
        if page > int(totalPage):
            return make_response(jsonify({'msg': '没有跟多数据了'}), 202)

        list_sql = """
SELECT  * 
FROM (select ROW_NUMBER() OVER(ORDER BY zc_no DESC) AS ROWNUMBER, zc_no AS VALUE,zc_no+';'+name AS LABEL,0 AS [SELECT] from zc_no where zc_no!='' {0}) TEMP_ROW
WHERE ROWNUMBER>((:page - 1) * :pageSize) and ROWNUMBER<=((:page) * :pageSize)
            """.format(parmStr)
        # print(list_sql)
        # print(pmr)
        list_d, err = dbCurdD.dbListJon(list_sql, pmr)
        if err != None:
            # print(err)
            return make_response(jsonify({'msg': '明细查询失败:' + err}), 202)
        # print(data)
        # dataList = [
        #     {"ti_no":"TI20230609001","cus_no":"a0001","cus_name":"广州天心软件科技有限公司","ti_dd":"2023-06-09","select":False},
        #     {"ti_no":"TI20230609002","cus_no":"a0002","cus_name":"深圳天心软件科技有限公司aaaadasdsadasdarewasfsfsdf","ti_dd":"2023-06-10","select":False}
        # ]
        previousPage = False if page <= 1 else True
        nextPage = False if page >= totalPage else True
        result = {}
        result['data'] = list_d
        # 分页大小
        result['pageSize'] = pageSize
        # 是否有上一页
        result['previousPage'] = previousPage
        # 是否有下一页
        result['nextPage'] = nextPage
        # 总分页
        result['totalPage'] = totalPage
        # 当前页
        result['page'] = page
        # 总记录
        result['total'] = count_d['total']
        msg = {'msg': '成功', "result": result}
        return make_response(jsonify(msg), 200)


